﻿function Storyboard(target, width, height) {

    this.width = width;             // the width of each sprite sheet element
    this.height = height;           // the height of each sprite sheet element
    this.rowLengths = [];           // the length of each row
    this.isRunning = false;         // whether this instance is running
    this.currentFrame = 0;          // the current frame in the sprite sheet
    this.currentRow = 0;            // the current row in the sprite sheet
    this.target = target;           // the targetted html element
    this.completedHandler = null;   // the handler for storyboard completion
    this.isReverse = false;         // whether the storyboard is reverse

    this.target.storyboard = this;

    if (typeof Storyboard.initialized == "undefined") {
        Storyboard.prototype.PushRow = function (rowLength) {
            this.rowLengths.push(rowLength);
        };

        Storyboard.prototype.Begin = function (row) {
            if (row != "undefined") { this.currentRow = row; }
            site.animationManager.Begin(this);
        };
        Storyboard.initialized = true;
    }
};


site.animationManager = {
    clock: null,
    animations: [],

    // Begins a storyboard
    Begin: function (storyboard) {
        //if (!storyboard.isRunning) {
        storyboard.isRunning = true;
        storyboard.isReverse = false;
        storyboard.currentFrame = 0;
        site.animationManager.ApplyFrame(storyboard);
        if ($.inArray(storyboard, site.animationManager.animations) == -1) {
            site.animationManager.animations.push(storyboard);
        }
        site.animationManager.CheckClock();
        //}
    },
    ClockIteration: function () {
        for (var index in site.animationManager.animations) {
            var item = site.animationManager.animations[+index];
            if (!item.isReverse) { // Normal case
                if (++(item.currentFrame) >= +item.rowLengths[+item.currentRow]) {
                    item.currentFrame = +item.rowLengths[item.currentRow] - 1;
                    item.isRunning = false;
                    if (item.completedHandler) {
                        item.completedHandler();
                    }
                    site.animationManager.animations.splice(+index, 1);
                }
            } else { // Reverse case
                if (--(item.currentFrame) < 0) {
                    item.currentFrame = +0;
                    item.isRunning = false;
                    if (item.completedHandler) {
                        item.completedHandler();
                    }
                    site.animationManager.animations.splice(+index, 1);
                }
            }
            site.animationManager.CheckClock();
            site.animationManager.ApplyFrame(item);
        }
    },
    ApplyFrame: function (storyboard) {
        $(storyboard.target).css("background-position", -(storyboard.currentFrame * storyboard.width) + "px " + -(storyboard.currentRow * storyboard.height) + "px");
    },
    CheckClock: function () {
        if (site.animationManager.clock == null && site.animationManager.animations.length > 0) {
            site.animationManager.clock = setInterval(site.animationManager.ClockIteration, 1000 / 25);
        } else if (site.animationManager.animations.length == 0 && site.animationManager.clock) {
            clearInterval(site.animationManager.clock);
            site.animationManager.clock = null;
        }
    }
};